/**
 * Copyright (C) FPT - Aptech 
 * BaseDao.java - 2012 - khangpm
 */
package dao;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.DatabaseProperties;

/**
 * BaseDao connect to database sql server
 * <p/>
 * @author khangpm
 */
public class BaseDao {

    protected Connection connection = null;
    protected PreparedStatement preparedStatement = null;
    protected Statement statement = null;
    protected ResultSet resultSet = null;
   
    public BaseDao() {
    }
    /**
     * getConnect open connection
     * @return boolean
     */
    public boolean getConnect() {
        boolean isConnect = true;
        try {
            Class.forName(DatabaseProperties.getData("driver"));
            String urlDB = DatabaseProperties.getData("url") + DatabaseProperties.getData("server")
                    + ":" + DatabaseProperties.getData("port") + "; DatabaseName = " + DatabaseProperties.getData("database");
           
            connection = DriverManager.getConnection(urlDB, DatabaseProperties.getData("user"), DatabaseProperties.getData("password"));
        } catch (Exception ex) {           
            isConnect = false;
        }
        return isConnect;
    }
    /**
     * close connect
     */
    public void disconnect() {
        if (resultSet != null) {
            try{
                resultSet.close();
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
        
        if (statement != null) {
            try{
                statement.close();
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
        
        if (preparedStatement != null) {
            try{
                preparedStatement.close();
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
        
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    
}
